//
//  SendViewController.m
//  MCBLESDK
//
//  Created by Jany on 2017/12/26.
//  Copyright © 2017年 MillionConcept. All rights reserved.
//

#import "SendViewController.h"
#import "SendLeftTableViewCell.h"
#import "SendTableViewCell.h"
#import "CommandModel.h"
#import "MCBLE.framework/Headers/MCCommands.h"
#import "MCBLE.framework/Headers/AlarmSetting.h"
#import "MCBLE.framework/Headers/NoDisturbingSetting.h"
#import "MCBLE.framework/Headers/SedentaryRemind.h"
#import "MCBLE.framework/Headers/HistoryDataTime.h"
#import "MCBLE.framework/Headers/HardwareInformation.h"
#import "MCBLE.framework/Headers/DeviceTime.h"
#import "MCBLE.framework/Headers/GetUserInformation.h"
#import "MCBLE.framework/Headers/SleepTime.h"
#import "MCBLE.framework/Headers/HistoryData.h"

@interface SendViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度所用字典
@property (weak, nonatomic) IBOutlet UITableView *myTableview;
@property (weak, nonatomic) IBOutlet UILabel *commandLabel;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

@implementation SendViewController{
    NSMutableArray *_commandsArr;
    BOOL _falg;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _commandsArr = [NSMutableArray arrayWithCapacity:10];
    _heightAtIndexPath = [[NSMutableDictionary alloc] init];
    self.title = title;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendData:) name:MCNotificationAtDidWriteValue object:nil];

    _falg = YES;
    
    [[MCCommands sharaCommands] getHeartRateMeasureData:^(NSInteger bpmValue) {
        NSLog(@"=======%d",bpmValue);
    }];
    
    [[MCCommands sharaCommands] getRealTimeData:^(NSData *data) {
        NSLog(@"=======%@",data);
    }];
}


#pragma mark==================================构造model信息==================================
- (void)makeUpMessage:(NSString *)message{
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_commandsArr.count inSection:0];
    [indexPaths addObject:indexPath];
    
    CommandModel *model = [[CommandModel alloc] init];
    model.flag = NO;
    model.commandData = nil;
    model.commandTime = [[NSDate date] timeIntervalSince1970];
    model.message = message;
    [_commandsArr addObject:model];
    
    [_myTableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    
    [_myTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_commandsArr.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark==================================返回删掉通知==================================
- (void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MCNotificationAtDidWriteValue object:nil];
    [[MCCommands sharaCommands] getHeartRateMeasureData:nil];
    [[MCCommands sharaCommands] getRealTimeData:nil];
}

#pragma mark==================================通知回调==================================
- (void)sendData:(NSNotification *)notify{
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_commandsArr.count inSection:0];
    [indexPaths addObject:indexPath];
    
    NSDictionary *dic = notify.object;
    
    _commandLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"data"]];
    CBCharacteristic *c = [dic objectForKey:@"c"];
    _myLabel.text = [NSString stringWithFormat:@"%@",c.UUID];
    
    CommandModel *model = [[CommandModel alloc] init];
    model.flag = NO;
    model.commandData = [dic objectForKey:@"data"];
    model.commandTime = [[NSDate date] timeIntervalSince1970];
    model.message = [NSString stringWithFormat:@"%@",[dic objectForKey:@"data"]];
    [_commandsArr addObject:model];
    
    [_myTableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
    
    [_myTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_commandsArr.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark==================================接口验证==================================
- (IBAction)sendClick:(id)sender {
    
    if (![MCBLEManager shareMCBluetooth]->connectFlag) {
        return;
    }
    __weak typeof (self) weakSelf = self;
    
    switch (index) {
        case 0:
        {
            [[MCCommands sharaCommands] sendBasicSetOfInformationData:YES withActivityTimeZone:8 success:^(NSData *data) {
                
                [weakSelf createModel:data];
                
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
            
        }
            break;
        case 1:
        {
            [[MCCommands sharaCommands] sendLocalTimeInformationData:[NSDate date] success:^(NSData *data) {
                
                [weakSelf createModel:data];
                
            } fail:^{
                
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 2:
        {
            [[MCCommands sharaCommands] sendUserInformationBodyDataWithAge:20 withWeight:120 height:160 sex:YES withTarget:1000 withStepAway:20 withSleepTarget:100 success:^(NSData *data) {
                [weakSelf createModel:data];
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 3:
        {
            [[MCCommands sharaCommands] sendRequestBasicSetOfInformationDataSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
                DeviceTime *model = [[DeviceTime alloc] initWithData:data];
                NSLog(@"------%@",model.deviceDate);
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 4:
        {
            [[MCCommands sharaCommands] sendLookBodyInformationDataWithUpdateSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
                GetUserInformation *model = [[GetUserInformation alloc] initWithData:data];;
                NSLog(@"------%d",model.targetStepCount);
                
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 5:
        {
            [[MCCommands sharaCommands] sendSetHardwareScreenDataWithDisplayFlag:YES waitingFlag:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 6:
        {
            [[MCCommands sharaCommands] sendPasswordProtectionDataWithOpen:YES withPassword:@"123456" success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 7:
        {
            [[MCCommands sharaCommands] sendDeviceResetSignalSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 8:
        {
            [[MCCommands sharaCommands] sendCheckDeviceSleepTimeSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
                SleepTime *model = [[SleepTime alloc] initWithData:data];
                NSLog(@"------%@",model.planSleepTime);
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 9:
        {
            [[MCCommands sharaCommands] sendQueryDeviceInterfaceAndDoNotDistrubTheFunctionSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
                NoDisturbingSetting *model = [[NoDisturbingSetting alloc] initWithData:data];
                NSLog(@"-------%d",model.showLogoFlag);
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 10:
        {
            [[MCCommands sharaCommands] sendDeviceAlarmTimeAndUseFuctionSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
                AlarmSetting *model = [[AlarmSetting alloc] initWithData:data];
                NSLog(@"------%@",model.alarmSettingArr);
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 11:
        {
            [[MCCommands sharaCommands] sendSetWearingWayDataWithRightHand:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 12:
        {
            [[MCCommands sharaCommands] sendDeviceSedentaryRemindSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                SedentaryRemind *model = [[SedentaryRemind alloc] initWithData:data];
                NSLog(@"------%d",model.openFlag);
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 13:
        {
            [[MCCommands sharaCommands] sendAutoHRTest:YES time:@[] success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 14:
        {
            [[MCCommands sharaCommands] sendQuaryHeartRateMeasureTimeSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 15:
        {
            [[MCCommands sharaCommands] sendRequestHistorySportDataWithDate:[NSDate date] success:^(NSData *data) {
                [weakSelf createModel:data];
                HistoryData *model = [[HistoryData alloc] initWithData:data];
                NSLog(@"=======%@",model);
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 16:
        {
            [[MCCommands sharaCommands] sendDeleteSportDataWithDate:[NSDate date] success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 17:
        {
            [[MCCommands sharaCommands] sendRealTimeGetDeviceDataSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 18:
        {
            [[MCCommands sharaCommands] sendHistoricalDataStorageDateWithUpdateSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                HistoryDataTime *model = [[HistoryDataTime alloc] initWithData:data];
                NSLog(@"------%@",model.startDate);
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 19:
        {
            [[MCCommands sharaCommands] requestDetailsAtIndex:10 success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 20:
        {
            [[MCCommands sharaCommands] requestDeviceOpenOrCloseFunctionCameraFlag:YES lockViewFlag:YES shakeFlag:YES findViewFlag:YES stepALGFlag:YES musicViewFlag:YES blueControlViewFlag:YES privacyFlag:YES haveBackViewFlag:YES HRShakeFlag:YES callFlag:YES HRFullFlag:YES pinNumberFlag:YES calariShowFlag:YES calariMeasureType:MCType sleepFunctionFlag:YES remindHaveIconFlag:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 21:
        {
            [[MCCommands sharaCommands] sendDeviceConstraintChargingSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 22:
        {
            [[MCCommands sharaCommands] sendOpenCameraOrderState:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 23:
        {
            [[MCCommands sharaCommands] sendAnswerDeviceMusicFuction];
        }
            break;
        case 24:
        {
            [[MCCommands sharaCommands] requestHardwareVirsionSuccess:^(NSData *data) {
                [weakSelf createModel:data];
                HardwareInformation *model = [[HardwareInformation alloc] initWithData:data];
                NSLog(@"-----%@",model);
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 25:
        {
            [[MCCommands sharaCommands] requestDeviceOpenANCSSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 26:
        {
            [[MCCommands sharaCommands] requestOpenLostFunctionSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 27:
        {
            [[MCCommands sharaCommands] requestCloseLostFunctionSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 28:
        {
            [[MCCommands sharaCommands] requestDeviceShakeSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 29:
        {
            [[MCCommands sharaCommands] sendReceiveDeviceAlarm];
        }
            break;
        case 30:
        {
            [[MCCommands sharaCommands] sendDeviceName:@"Jany" isBLE:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 31:
        {
            [[MCCommands sharaCommands] requestEnterFactoryModelSuccess:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 32:
        {
            [[MCCommands sharaCommands] sendAlertWhenConnectShakeCount:2 hook:YES success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 33:
        {
            [[MCCommands sharaCommands] sendPerpareRestDeviceAtIndex:1 success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 34:
        {
            [[MCCommands sharaCommands] sendPerpareRestDeviceAgain];
        }
            break;
            
        case 35:
        {
            [[MCCommands sharaCommands] requestDeviceEnterOTA];
        }
            break;
        case 36:
        {
            [[MCCommands sharaCommands] setSleepTimeOpenFlag:YES planSleepTime:[NSDate date] openPlanSleepTimeFlag:YES remindSleepTime:[NSDate date] remindSleepTimeFlag:YES awakeTime:[NSDate date] awakeTimeFlag:YES noonSleepTime:[NSDate date] noonSleepTimeFlag:YES noonSleepOverTime:[NSDate date] noonSleepOverTimeFlag:YES noonSleepRemindTime:[NSDate date] noonSleepRemindTimeFlag:YES success:^(NSData *data) {
                [weakSelf createModel:data];
                
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        case 37:
        {
            [[MCCommands sharaCommands] sendSetDistrubTheFunction:NO calorieFlag:NO sportDistance:NO sportTimeFlag:NO progressFlag:NO faceFlag:NO alarmFlag:NO messageFlag:NO callRemindFlag:NO messageRemindViewFlag:NO messageRemindType:MessageRemindNothing lifeTimeOverFlag:NO sportCountDownFlag:NO success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
        case 38:
        {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:12],@"startHour",[NSNumber numberWithInteger:30],@"startMinute",[NSNumber numberWithInteger:14],@"endHour",[NSNumber numberWithInteger:25],@"endMinute", nil];
            [[MCCommands sharaCommands] sendSetDeviceSedentaryRemindOpenState:YES setTimeArrary:@[dic] quietTime:50 success:^(NSData *data) {
                [weakSelf createModel:data];
            } fail:^{
                [weakSelf makeUpMessage:@"没有数据返回，可能已经断开或者协议和设备不统一"];
            }];
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark==================================构造接口回调的数据信息==================================
- (void)createModel:(NSData *)data{
    
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_commandsArr.count inSection:0];
    [indexPaths addObject:indexPath];
    
    CommandModel *model = [[CommandModel alloc] init];
    model.flag = YES;
    model.commandData = data;
    model.commandTime = [[NSDate date] timeIntervalSince1970];
    model.message = [NSString stringWithFormat:@"%@",data];
    [_commandsArr addObject:model];
    
    [_myTableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
    
    [_myTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_commandsArr.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark==================================UITableViewDataSource,UITableViewDelegate==================================
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _commandsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *leftIdentify = @"LeftCell";
    static NSString *rightIdentify = @"RightCell";
    CommandModel *model = _commandsArr[indexPath.row];
    if (model.flag) {//device
        SendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:leftIdentify];
        if (!cell) {
            cell = [[SendTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:leftIdentify];
        }
        cell.titleLabel.text = model.message;
        return cell;
    }else{
        SendLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightIdentify];
        if (!cell) {
            cell = [[SendLeftTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:rightIdentify];
        }
        cell.titleLabel.text = model.message;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *height = @(cell.frame.size.height);
    [self.heightAtIndexPath setObject:height forKey:indexPath];
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
    if(height){
        return height.floatValue;
    }else{
        return 60;
    }
}
@end
